package org.jeecg.modules.demo.saleorder.service.impl;

import org.jeecg.modules.demo.saleorder.entity.SoSaleorder;
import org.jeecg.modules.demo.saleorder.entity.SoSaleorderB;
import org.jeecg.modules.demo.saleorder.mapper.SoSaleorderBMapper;
import org.jeecg.modules.demo.saleorder.mapper.SoSaleorderMapper;
import org.jeecg.modules.demo.saleorder.service.ISoSaleorderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 销售订单主实体
 * @Author: jeecg-boot
 * @Date:   2024-03-01
 * @Version: V1.0
 */
@Service
public class SoSaleorderServiceImpl extends ServiceImpl<SoSaleorderMapper, SoSaleorder> implements ISoSaleorderService {

	@Autowired
	private SoSaleorderMapper soSaleorderMapper;
	@Autowired
	private SoSaleorderBMapper soSaleorderBMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(SoSaleorder soSaleorder, List<SoSaleorderB> soSaleorderBList) {
		soSaleorderMapper.insert(soSaleorder);
		if(soSaleorderBList!=null && soSaleorderBList.size()>0) {
			for(SoSaleorderB entity:soSaleorderBList) {
				//外键设置
				entity.setIdH(soSaleorder.getId());
				soSaleorderBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(SoSaleorder soSaleorder,List<SoSaleorderB> soSaleorderBList) {
		soSaleorderMapper.updateById(soSaleorder);
		
		//1.先删除子表数据
		soSaleorderBMapper.deleteByMainId(soSaleorder.getId());
		
		//2.子表数据重新插入
		if(soSaleorderBList!=null && soSaleorderBList.size()>0) {
			for(SoSaleorderB entity:soSaleorderBList) {
				//外键设置
				entity.setIdH(soSaleorder.getId());
				soSaleorderBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		soSaleorderBMapper.deleteByMainId(id);
		soSaleorderMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			soSaleorderBMapper.deleteByMainId(id.toString());
			soSaleorderMapper.deleteById(id);
		}
	}
	
}
